import { Injectable } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { HttpClient, HttpParams } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class AccessstoreListService {
  constructor(
    private http: HttpClient,
    private message: NzMessageService) { }
  // private baseUrl = 'http://192.168.31.254:8000'
 private baseUrl = 'http://202.99.212.205:8000'
 //private baseUrl = 'http://10.144.1.53:8000'
  private header = {
    Authorization: localStorage.getItem("token")
  }
  // 查询
  private searchUrl = this.baseUrl + '/api/roles'
  async getGetDateList(data): Promise<Node> {
    let dataJson = {
      blurry: data.blurry,
      createTime: data.createTime,
      page: data.page,
      size: data.size
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.get(this.searchUrl, { headers: this.header, params: dataJson }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        let errorMessage = err.error.message
        this.message.create("error", errorMessage)
        reject(err)
      })
    })
  }
  //新增
  private addUrl = this.baseUrl + '/api/add'
  async add(data): Promise<Node> {
    let dataJson = {
      dataScope: data.dataScope,
      level: data.level,
      name: data.name,
      permission: data.permission,
      remark: data.remark,
      depts: data.depts
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.post(this.addUrl, dataJson, { headers: this.header }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        let errorMessage = err.error.message
        this.message.create("error", errorMessage)
        reject(err)
      })
    })
  }
  //修改
  private editUrl = this.baseUrl + '/api/edit'
  async edit(data): Promise<Node> {
    let dataJson = {
      dataScope: data.dataScope,
      level: data.level,
      name: data.name,
      permission: data.permission,
      remark: data.remark,
      depts: data.depts
    }
    return new Promise<Node>((resolve, reject) => {
      this.http.post(this.editUrl, dataJson, { headers: this.header }).subscribe((res: any) => {
        resolve(res);
      }, err => {
        let errorMessage = err.error.message
        this.message.create("error", errorMessage)
        reject(err)
      })
    })
  }
}
